<?php
	defined("AT_EXE") or die ("CORE VARIABLE IS NOT DEFINED.");
	Import::core("Request");
	class Route extends AT {
		private $request=NULL;
		private $routes=array();
		/*
		 * start route request
		 */
		public function spread(){
			$this->read();
			if($this->validateRoute()){
				$route = $this->routes[$this->request->getController().$this->request->getAction()] ;
				foreach($route["controller"] AS $controller => $action ){
					$controller = $this->loadController($controller);
					//if($route["request"]->getAccess() ){
						$controller->setRoute($route);
						$controller->setDatas($this->request->getVars());
						!method_exists($controller,$action)?die("Error : ".get_class($controller)." does not has method '".$action."'"):false;
						$countor = $this->loadModel("Countor");
						$countor->increaseCountor();
						call_user_func(array($controller,$action));
					/*}else{
						Basic::refresh( $route["request"]->getRedirectPath() ,array() , 3 );
						echo "You don't have right to access this page, or your session on server is expired. !!!" ;
					}*/
					break;
				}
			}
		} 
		/*
		 * Read request
		 */
		private function read(){
			$this->request=new Request();
			if(isset($_SERVER["QUERY_STRING"])&&$_SERVER["QUERY_STRING"]!==""){
				// work with Query String
				parse_str($_SERVER['QUERY_STRING'], $outputs);
				$com=isset($outputs["com"])?$outputs["com"]:"page";
				$action=isset($outputs["action"])?$outputs["action"]:"index";
				$this->request->setComponent(array($com=>$action));
				isset($_POST)&&!empty($_POST)?$outputs=array_merge($outputs,$_POST):false;
				unset($outputs['com']);
				unset($outputs['action']);
				$this->request->setVars($outputs);
			}
			else if(isset($_SERVER["REQUEST_URI"])&&$_SERVER["REQUEST_URI"]!==""){
				$paths = ROOT!=""&&ROOT!=DS?array(ROOT,ROOT.DS,ROOT.DS."index.php",ROOT.DS."index.php".DS):array(ROOT.DS."index.php",ROOT.DS."index.php".DS,"index.php".DS,"index.php");
				$outputs=array_slice(explode("/",str_replace($paths,"",$_SERVER["REQUEST_URI"])),1);
				$com=isset($outputs[0])&&$outputs[0]!==""?$outputs[0]:"page";
				$action=isset($outputs[1])&&$outputs[1]!==""?$outputs[1]:"index";
				unset($outputs[0]);
				unset($outputs[1]);
				$vars = array();
				foreach($outputs AS $output ){
					if(strpos($output,":")!==false){
						list($key,$val)=explode(":",$output);
						$vars = array_merge($vars,array($key=>$val));
					}
				}
				$this->request->setComponent(array($com=>$action));
				isset($_POST)&&!empty($_POST)?$vars=array_merge($vars,$_POST):false;
				$this->request->setVars($vars);
			}
			else{
				$this->request->setComponent(array("page"=>"index"));
			}
		}
		public function setDefault($default=array()){
			$this->defaultController = $default;
		}
		public function getDefault(){
			return $this->defaultController;
		}
		public function setRoute($route=NULL){
			(is_array($route)&&!empty($route))?$this->routes[$route["request"]->getController().$route["request"]->getAction()]=$route:die("Class : Route<br/>Function : setRequest();<br/>Error set request.");
		}
		public function validateRoute(){
			return Basic::keyExist($this->request->getController().$this->request->getAction(), $this->routes )?true:die("Error : we don't support the requested url.");
		}
	}
